package com.kindnesslibrary.activity.adapter;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.kindnesslibrary.R;
import com.kindnesslibrary.adapter.CommonAdapter;
import com.kindnesslibrary.adapter.ViewHolder;
import com.kindnesslibrary.model.XImageModel;
import com.kindnesslibrary.ximage.XImage;
import com.kindnesslibrary.ximage.XImgSelConfig;

import java.util.List;

/**
 * Created by lenovo on 2017/6/3.
 * XImage适配器，装载图片
 */
public class XImageAdapter extends CommonAdapter<XImageModel> {

    private OnViewItemClickListener onViewItemClickListener;
    private int status = RecyclerView.SCROLL_STATE_IDLE;

    public XImageAdapter(Context context, int layoutId, List<XImageModel> datas) {
        super(context, layoutId, datas);
    }

    @Override
    public void convert(ViewHolder holder, XImageModel xImageModel) {
        ImageView ivXimageImage = holder.getView(R.id.iv_ximage_image);
        ImageView ivXimageCheck = holder.getView(R.id.iv_ximage_check);
        View v_ximage_back = holder.getView(R.id.v_ximage_back);
        v_ximage_back.setBackgroundColor(Color.parseColor("#666666"));
        XImgSelConfig imgSelConfig = XImage.getConfig();
        // 如果当前item被选中，则显示选中图片
        if(imgSelConfig.selectImage.contains(xImageModel.filePath)){
            ivXimageCheck.setImageBitmap(BitmapFactory.decodeResource(ivXimageCheck.getResources(), R.mipmap.ximage_check));
            v_ximage_back.setAlpha(0.5f);
        }else{
            ivXimageCheck.setImageBitmap(BitmapFactory.decodeResource(ivXimageCheck.getResources(), R.mipmap.ximage_no));
            v_ximage_back.setAlpha(0f);
        }
        if(imgSelConfig.loader != null) {
            imgSelConfig.loader.load(ivXimageCheck.getContext(), xImageModel.filePath, ivXimageImage, this, holder.getIndex());
        }
        if(imgSelConfig.maxNum > 1){
            ivXimageCheck.setVisibility(View.VISIBLE);
        }else{
            ivXimageCheck.setVisibility(View.GONE);
        }
        ivXimageImage.setOnClickListener(v -> {
            if(onViewItemClickListener != null){
                xImageModel.position = holder.getIndex();
                onViewItemClickListener.onClick(xImageModel);
            }
        });
    }

    public void setOnViewItemClickListener(OnViewItemClickListener onViewItemClickListener) {
        this.onViewItemClickListener = onViewItemClickListener;
    }

    public interface OnViewItemClickListener{
        void onClick(XImageModel xImageModel);
    }

    public void updateStatus(int status){
        this.status = status;
    }

    public int getStatus(){
        return status;
    }

}
